%% Using the Statistic Toobox RNGs

%These are part of MATLAB (No toolbox needed)
rand
randn

%These are part of the Statistics Toolbox

%help normrnd
normrnd(0,1); %Same as randn
normrnd(zeros(10,1),1) 

%and we have more distributions available
exprnd(ones(10,1))
gamrnd(1,3)
poissrnd(ones(10,1))


% Not only do have have RNGs, we also have PDFs and CDFs

P=poisspdf((0:10),2)
figure
plot((0:10),P)
xlabel('X')
ylabel('Poisson PDF')

C=poisscdf((0:10),2)
figure
plot((0:10),C)
xlabel('X')
ylabel('Poisson CDF')

%We can also make distribution 'objects' and do even more
pd = makedist('Normal')
pd.random
pd.pdf(0)
pd.cdf(1)
pd.mean
pd.std



%% Symbolic Toolbox Examples

%see 'doc sym' for many more examples

%Create a symbolic variable
x=sym('x')

%or: 
sym x

%y is function of x and is also symbolic
y=x^2

y^2

%Indefinite Integral
Y=int(y)

%Definite Integral
Y_Def=int(y,0,1)  


%Derivative
y_p=diff(y)

%Second derivative
y_p2=diff(y,2)

%'diff' operates differently on symbolic or numerical arrays. 
diff((0:10))


%% Plotting

figure
ezplot(y,[-10 10]) %We have 'ez' versions of all the plotting functions
hold on
ezplot(y_p,[-10 10]) %we give the limits of plotting as second arg.
ezplot(Y,[-10 10])
legend('y','y_p','Y')

syms t
figure
ezplot3(t^2*sin(10*t), t^2*cos(10*t), t)

syms x y
figure
ezsurf(x^2 + y^2)
hold on
zlabel('z')
title('z = x^2 + y^2')
hold off

%many other 'ez' versions of standard plots


%% Solving Equations
clear 
syms a b c y x

%Solve an equation
Out=solve( a*x^2+b*x+c==0,x)
Out(1)
Out(2)

%Solve system of equations
Out=solve(x-y==5, x^2+y==4,[x,y])

%Get results
Out.x
Out.y

Out.y(1)
Out.x(1)

%Try to simplify expression
simplify(Out.y(1))
X=double(Out.y(1))

simplify(Out.x)

%Check the results to see they satisfy equations above
Out.x(1)-Out.y(1)
Out.x(1).^2+Out.y(1)
simplify(Out.x(1).^2+Out.y(1))

%Convert to numeric
double(Out.x)


%% Solving ODEs

% Wave equation
clear 
syms y(t)  
D2y = diff(y,2);
Ysol=dsolve(D2y == -y)

% Exponential
syms y(t) k 
Dy = diff(y);
Ysol=dsolve(Dy == -k*y)

%with boundary condition
Ysol=dsolve(Dy == -k*y,y(0)==100)

%put in a specific 'k' and plot
Ysubk=subs(Ysol,k,3)

figure
ezplot(Ysubk,[0 5 0 100])




%% Create function handle and use with numeric data

clear 
syms x y
r = sin(x);
ht = matlabFunction(sin(r)/r)

Xvec=linspace(0,10,100)
Yvec=ht(Xvec)
figure
plot(Xvec,Yvec)
xlabel('X')
ylabel('Y')

%% Show latex

%The 'text' function allows us to put text somewhere on a figure
figure
text(1,1,'asdf')

%We can use this for showing latex equations
latex(Ysol)
figure
h = text('string',['$' latex(Ysol) '$'],...
             'interpreter','latex',...
             'fontsize',30,...
             'units','norm',...
             'pos',[.1 .5]);
       
% Taylor Series
syms x
f = 1/(5 + 4*cos(x));
T = taylor(f, 'Order', 8)

%Make look nice in the command window
pretty(T)

%Same in Latex
figure
h = text('string',['$' latex(T) '$'],...
             'interpreter','latex',... 
             'fontsize',30,...
             'units','norm',...
             'pos',[.1 .5]);












